import { getToken, setToken, removeToken } from '@/utils/auth'
import { login } from '@/api/user'
// import { TOKEN_KEY } from '@/utils/constants'
const state = {
  token: getToken() || ''
}
const mutations = {
  setToken(state, token) {
    // 设置token，将token保存到state中
    state.token = token
    // 将token保存到cookie中
    setToken(token)
  },
  removeToken(state) {
    state.token = ''
    removeToken()
  }
}
const actions = {
  async login(context, { username, password }) {
    const res = await login({ username, password })
    console.log(res.data.token)
    context.commit('setToken', res.data.token)
  }
}
const getters = {}
export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
